package com.phonegap.breastmilk;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.phonegap.breastmilk.R;


import android.app.Dialog;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/** Class Must extends with Dialog */
/** Implement onClickListener to dismiss dialog when OK Button is pressed */
public class CustomizeDialog extends Dialog implements OnClickListener {
	Button okButton;

	public CustomizeDialog(Context context, Bundle bundle) {
		super(context);
		/** 'Window.FEATURE_NO_TITLE' - Used to hide the title */
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		/** Design the dialog in main.xml file */
		setContentView(R.layout.dialogcustom);
		
		okButton = (Button) findViewById(R.id.OkButtonDialog);
		okButton.setOnClickListener(this);
		
        int id = bundle.getInt("ID");
        SQLiteDatabase db = Connection.getInstance(context).getReadableDatabase();
        String from[] = new String [] {BreastmilkSQLiteHelper.ID,BreastmilkSQLiteHelper.DATE_BEGIN,BreastmilkSQLiteHelper.DATE_END,BreastmilkSQLiteHelper.BREAST_FED,BreastmilkSQLiteHelper.NOTES};
        Cursor cursor = db.query(
    			BreastmilkSQLiteHelper.DATABASE, 
    			from,
    			"_id = "+id, null, null, null, null);
        
        if (cursor.moveToFirst()) {
        	String date_begin = cursor.getString(1);
            String date_end = cursor.getString(2);
            String breast = cursor.getString(3);
            String notes = cursor.getString(4);
            
            // Date
            Pattern p = Pattern.compile("([\\w]+\\s[\\w]+\\s[0-9]+)\\s([0-9]+:[0-9]+:[0-9]+).*\\s([0-9]+)$");
    		Matcher m1 = p.matcher(date_begin);
    		if (m1.find()) {
    			((TextView)findViewById(R.id.textViewValDate)).setText(m1.group(1));
    		}
    		// Start Time
    		Matcher m2 = p.matcher(date_begin);
    		if (m2.find()) {
    			((TextView)findViewById(R.id.textViewValStartTime)).setText(m2.group(2));
    		}
    		
    		// End Time
    		Matcher m3 = p.matcher(date_end);
    		if (m3.find()) {
    			((TextView)findViewById(R.id.textViewValEndTime)).setText(m3.group(2));
    		}
    		
    		DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
    		String duration = "";
    		try {
				Date startDate = sdf.parse(m2.group(2));
				Date endDate = sdf.parse(m3.group(2));
				long diff = endDate.getTime() - startDate.getTime();
				Log.i("diff", String.valueOf(diff));
				int seconds = (int) ((diff / 1000) % 60);
				int minutes = (int) ((diff / (1000*60)) % 60);
				int hours = (int) (int) ((diff / (1000*60*60)) % 24);;
				duration = ((hours < 10) ? "0" + String.valueOf(hours) : String.valueOf(hours)) +
							":" + ((minutes < 10) ? "0" + String.valueOf(minutes) : String.valueOf(minutes)) +
							":" + ((seconds < 10) ? "0" + String.valueOf(seconds) : String.valueOf(seconds));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			((TextView)findViewById(R.id.textViewValDuration)).setText(duration);
    		// Breast
    		((TextView)findViewById(R.id.textViewValBreast)).setText(breast);
    		// Notes
    		((TextView)findViewById(R.id.textViewValMyNotes)).setText(notes);
        }
        cursor.close();
        db.close();
	}

	@Override
	public void onClick(View v) {
		/** When OK Button is clicked, dismiss the dialog */
		if (v == okButton)
			dismiss();
	}

}
